/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2022 All Rights Reserved.
 */
package com.example.spider.proxy.provider;

import com.example.spider.proxy.core.ProxyInfo;
import com.example.spider.proxy.core.tester.ProxyTypeEnum;
import com.example.spider.starter.SpiderStarter;
import com.example.spider.task.SpiderTaskQueue;

import java.util.Arrays;

/**
 * ip:port
 * <p>
 * https://github.com/roosterkid/openproxylist
 *
 * @author lym
 */
//@Component
public class IpPortTxtResultHandler extends AbstractProxyResultHandler implements SpiderStarter {

    public IpPortTxtResultHandler() {
        super("ipPortText");
    }

    @Override
    public void startSpiders() {
        // 21年没再更新过 不建议用了
//        IpProxySpiderTask goodText = createTask("https://www.rmccurdy.com/.scripts/proxy/good.txt")
//                .setSource("rmccurdy");
//        SpiderTaskQueue.putTask(goodText);

        //      https://github.com/mertguvencli/http-proxy-list/actions
        //https://github.com/mertguvencli/http-proxy-list
        //         每10min更新
        SpiderTaskQueue.putTask(createTask("https://raw.githubusercontent.com/mertguvencli/http-proxy-list/main/proxy-list/data.txt")
                .setSource("github_rmccurdy")
                .setType("HTTP")
        );

        //        1hour 更新
        //        https://github.com/roosterkid/openproxylist
        SpiderTaskQueue.putTask(
                createTask("https://raw.githubusercontent.com/roosterkid/openproxylist/main/HTTPS_RAW.txt")
                        .setSource("github_rmccurdy")
                        .setType("HTTPS")
        );
        SpiderTaskQueue.putTask(
                createTask("https://raw.githubusercontent.com/roosterkid/openproxylist/main/SOCKS4_RAW.txt")
                        .setSource("github_rmccurdy")
                        .setType("SOCKS4")
        );
        SpiderTaskQueue.putTask(
                createTask("https://raw.githubusercontent.com/roosterkid/openproxylist/main/SOCKS5_RAW.txt")
                        .setSource("github_rmccurdy")
                        .setType("SOCKS5")
        );
    }

    /**
     * 每 30min 触发一次
     */
    @Override
    public String executeCron() {
        return "0 */30 * * * ?";
    }


    /**
     * 处理响应
     */
    @Override
    public void doHandle(IpProxySpiderTask task, String result) {

        // 222.223.182.66:8000
        String[] ipPortStrArr = result.split("\n");

        Arrays.stream(ipPortStrArr).parallel().map(s -> ProxyInfo.builder()
                        .source(taskName)
                        .ip(s.split(":")[0])
                        .port(Integer.parseInt(s.split(":")[1]))
                        .proxyTypeEnum(ProxyTypeEnum.of(task.getType()))
                        // 兼容
                        .type(ProxyTypeEnum.of(task.getType()).getType())
                        .source(task.getSource())
                        .build())
                .forEach(super::addProxyToSourceRepository);
        ;
    }

}